﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;

using System.Text;
using System.Windows.Forms;
using System.Data.SQLite;
using System.Linq;
using Iprojects.Data;
namespace Iprojects.Forms
{
    public partial class ProjectFoodBoxes : Form
    {
      private Person  currentPerson;
         
        private ZakahEntities db=new ZakahEntities();
        int familyid = 0;
        public ProjectFoodBoxes()
        {
            InitializeComponent();
            dateTimePicker1.Value = DateTime.Now;
            dateTimePicker1.Format = DateTimePickerFormat.Custom;
            dateTimePicker1.CustomFormat = "dd/mm/yyyy";
            dtGridFamilyProjectHistory.AutoGenerateColumns = false;

         //   dtGridFamilyProjectHistory.Columns.Add("sum", "مجموع  ");
          

            
           
        }

        private void button1_Click(object sender, EventArgs e)
        {
           // add new project event to family 
         //   oPerson = new tblPersonsDB(vars.mstrCnn);
            FoodBoxProject newFamilyProj = new FoodBoxProject();

            newFamilyProj.amount = Convert.ToInt32(txtAmount.Text);
            newFamilyProj.familyID = familyid;
       //     newFamilyProj.checknumber = txtchecknum.Text;
            newFamilyProj.date = dateTimePicker1.Value.Date;
            //newFamilyProj.reason = txtReason.Text;

            db.AddToFoodBoxProjects(newFamilyProj);
            db.SaveChanges();
            btnAddFamily.Enabled = false;
            var query = from p in db.FoodBoxProjects.Where(x => x.familyID == familyid)
                        select p;


            dtGridFamilyProjectHistory.DataSource = query.ToList<FoodBoxProject>();
           
    
        }

           void txtid_Leave(object sender, System.EventArgs e)
        {

            Person WifePerson = new Person();

            // check if there is id like that at table FamilyPerson
            //if have a relation in family person then
            bool isthereahusbandinsystem = false;
            if (txtid.Text.Length > 8)
            {
                //  personDetails = this.oPerson.GetPersonDetailsbyID(txtid.Text);

                currentPerson = db.Persons.FirstOrDefault(x => x.IDnum == txtid.Text);
                FamilyPerson family_rel = db.FamilyPersons.FirstOrDefault(x => x.Husbandid == currentPerson.IDnum || x.wifeid == currentPerson.IDnum);
                familyid = Convert.ToInt32(family_rel.id);
                var query = from p in db.FoodBoxProjects.Where(x => x.familyID == familyid)
                            select p;
                WifePerson = db.Persons.FirstOrDefault(x => (x.FamilyID == familyid) && (x.IDnum != txtid.Text));  
             
               
                dtGridFamilyProjectHistory.DataSource = query.ToList<FoodBoxProject>();
            }


            if (null == currentPerson)
            {
                //  isthereahusbandinsystem
                MessageBox.Show("لم يتم العثور على الشخص");
                // clear id  
                txtid.Text = string.Empty;
                txtid.Focus();


            }
            else
            {
                //  DataRow row = personDetails.Rows[0];
                btnAddFamily.Enabled = true;
                //  txtFullname.Text =row["FirstName"].ToString() +" "+ row["FatherName"].ToString() + " "+  row["LastName"].ToString();
                txtFullname.Text = currentPerson.FirstName + " " + currentPerson.FatherName + " " + currentPerson.LastName;
                txtFullNameWife.Text = WifePerson.FirstName + " " + WifePerson.FatherName + " " + WifePerson.LastName;
                txtIdWife.Text = WifePerson.IDnum;
                familyid = Convert.ToInt32(currentPerson.FamilyID);
            

                // fill dtGridFamilyProjectHistory history  

                //oPerson.GetFamilyMoslemFamilyProjectbyID(familyid);

                int sum = 0;
                for (int i = 0; i < dtGridFamilyProjectHistory.Rows.Count; ++i)
                {
                    sum += Convert.ToInt32(dtGridFamilyProjectHistory.Rows[i].Cells["amount"].Value);
                }
                lblsumamount.Text = sum.ToString();

                txtAmount.Focus();
            }
           
        }
        }

   
    }



